package l2ft.gameserver.network.l2.c2s;

import java.io.IOException;

import l2ft.gameserver.Config;
import l2ft.gameserver.hwid.SmartGuard;
import l2ft.gameserver.network.l2.s2c.KeyPacket;
import l2ft.gameserver.network.l2.s2c.SendStatus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProtocolVersion extends L2GameClientPacket
{
	private static final Logger _log = LoggerFactory.getLogger(ProtocolVersion.class);

	private int _protocol;
	private int _systemVer;
	private byte[] _data;
	private String _hwidHdd = "", _hwidMac = "", _hwidCPU = "";

	protected void readImpl()
	{
		_protocol = readD(); 
		if ((_buf.remaining() > 260) && SmartGuard.isSmartGuardEnabled())
		{
			_data = new byte[260];
			readB(_data);
			
			/*_systemVer = Integer.parseInt(*/readS()/*)*/;
			_systemVer = 1;
			_hwidHdd = readS();
			_hwidMac = readS();
			_hwidCPU = readS();
		}
	}

	protected void runImpl() throws IOException
	{
		if(_protocol == -2)
		{
			_client.closeNow(false);
			return;
		}
		else if(_protocol == -3)
		{
			_log.info("Status request from IP : " + getClient().getIpAddr());
			getClient().close(new SendStatus());
			return;
		}
		else if(_protocol < Config.MIN_PROTOCOL_REVISION || _protocol > Config.MAX_PROTOCOL_REVISION)
		{
			_log.warn("Unknown protocol revision : " + _protocol + ", client : " + _client);
			getClient().close(new KeyPacket(null));
			return;
		}
		getClient().setSystemRev(_systemVer);
		
		if (SmartGuard.isSmartGuardEnabled())
		{
			switch (SmartGuard.getHWIDOption())
			{
				case 1:
					if (_hwidHdd == "")
					{
						_log.info("Status HWID HDD : NoPatch!!!");
						getClient().close(new KeyPacket(null));
					}
					else
					{
						getClient().setHWID(_hwidHdd);
					}
					break;
				case 2:
					if (_hwidMac == "")
					{
						_log.info("Status HWID MAC : NoPatch!!!");
						getClient().close(new KeyPacket(null));
					}
					else
					{
						getClient().setHWID(_hwidMac);
					}
					break;
				case 3:
					if (_hwidCPU == "")
					{
						_log.info("Status HWID : NoPatch!!!");
						getClient().close(new KeyPacket(null));
					}
					else
					{
						getClient().setHWID(_hwidCPU);
					}
				break;
			}
		}
		else
		{
			getClient().setHWID("NoGuard");
		}
		sendPacket(new KeyPacket(_client.enableCrypt()));
	}

	public String getType()
	{
		return getClass().getSimpleName();
	}
}